<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>pog-generator</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
        #app {
            margin-top: 25px;
        }
    </style>
</head>

<body>
<div id="app">
    <el-row :gutter="20">
        <el-col :span="18" :offset="3">
            <div class="grid-content bg-purple">
                <el-form :inline="true" :model="generatorData" class="demo-form-inline">
                    <el-form-item label="作者">
                        <el-input v-model="generatorData.author"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱">
                        <el-input v-model="generatorData.email"></el-input>
                    </el-form-item>
                    <el-form-item label="表前缀">
                        <el-input v-model="generatorData.tablePrefix"></el-input>
                    </el-form-item>
                    <el-form-item label="包名">
                        <el-input v-model="generatorData.packageName"></el-input>
                    </el-form-item>
                    <el-form-item label="模块名">
                        <el-input v-model="generatorData.moduleName"></el-input>
                    </el-form-item>
                    <el-form-item label="数据库">
                        <el-select v-model="databaseName">
                            <el-option v-for="item in dataBaseOptions" :key="item.value" :label="item.label"
                                       :value="item.value">
                            </el-option>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">生成</el-button>
                    </el-form-item>
                </el-form>
                <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
                          @selection-change="handleSelectionChange">
                    <el-table-column type="selection">
                    </el-table-column>
                    <el-table-column prop="tableName" label="表名">
                    </el-table-column>
                    <el-table-column prop="comment" label="注释" show-overflow-tooltip>
                    </el-table-column>
                </el-table>
            </div>
        </el-col>
    </el-row>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!-- import axios -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                generatorData: {
                    author: "pog",
                    email: 'pogooo@foxmail.com',
                    packageName: "com.pog",
                    moduleName: "generator",
                    tablePrefix: "xxxx_",
                    databaseName: "",
                    tableList: []
                },
                databaseName: '',
                dataBaseOptions: [],
                tableData: [],
                multipleSelection: []
            }
        },
        watch: {
            databaseName(newValue, oldValue) {
                this.fetchTableList(newValue)
                this.generatorData.databaseName = newValue
            }
        },
        created() {
            this.fetchDataBase()
        },
        methods: {
            fetchTableList(databaseName) {
                axios.get('http://localhost/generator/table-list', {
                    params: {
                        databaseName
                    }
                }).then(response => {
                    this.tableData = response.data.data
                })
            },
            fetchDataBase() {
                axios.get('http://localhost/generator/database-list').then(response => {
                    response.data.data.forEach(databaseName => {
                        this.dataBaseOptions.push({
                            label: databaseName,
                            value: databaseName
                        })
                    })
                })
            },
            onSubmit() {
                this.generatorData.tableList = this.multipleSelection
                let {author, email, packageName, moduleName, tablePrefix, databaseName, tableList} = this.generatorData
                axios({
                    method: 'post',
                    url: 'http://localhost/generator/generator',
                    data: {
                        author,
                        email,
                        packageName,
                        moduleName,
                        tablePrefix,
                        databaseName,
                        tableList
                    },
                    responseType: "blob"
                }).then(response => {
                    console.log(response)
                    let blob = new Blob([response.data], {type: 'application/zip'})
                    let url = window.URL.createObjectURL(blob)
                    const link = document.createElement('a') // 创建a标签
                    link.href = url
                    link.download = "pog-generator" // 重命名文件
                    link.click()
                    URL.revokeObjectURL(url) // 释放内存
                })
                // axios.post('http://localhost/generator/generator', {
                //     author,
                //     email,
                //     packageName,
                //     moduleName,
                //     tablePrefix,
                //     databaseName,
                //     tableList
                // })
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            }
        }
    });
</script>

</html>